/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * serverChoose.java
 *
 * Created on Jun 19, 2010, 9:53:18 AM
 */

package jvid;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author david
 */
public class serverChoose extends javax.swing.JDialog {

    /** Creates new form serverChoose */
    public serverChoose() {
        initComponents();
        videoTrans = null;
        out = null;
        in = null;
       
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        connect = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        ipaddress = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        port = new javax.swing.JTextField();
        feedback = new javax.swing.JLabel();
        disconnect = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Coloca la información del servidor...");
        setLocationByPlatform(true);
        setResizable(false);

        connect.setText("Conectar");
        connect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectActionPerformed(evt);
            }
        });

        cancelButton.setText("Cerrar");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Dirección IP del servidor:");

        ipaddress.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ipaddressActionPerformed(evt);
            }
        });

        jLabel2.setText("Puerto:");

        feedback.setFont(new java.awt.Font("Lucida Grande", 0, 10)); // NOI18N
        feedback.setText(" ");

        disconnect.setText("Desconectar");
        disconnect.setEnabled(false);
        disconnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                disconnectActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(disconnect)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 22, Short.MAX_VALUE)
                .add(connect)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(cancelButton)
                .add(17, 17, 17))
            .add(layout.createSequentialGroup()
                .add(20, 20, 20)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel2)
                    .add(jLabel1))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(ipaddress, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
                        .add(36, 36, 36))
                    .add(layout.createSequentialGroup()
                        .add(port, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 62, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(feedback, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 338, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(ipaddress, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(port, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(12, 12, 12)
                .add(feedback)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 15, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cancelButton)
                    .add(connect)
                    .add(disconnect))
                .add(9, 9, 9))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void ipaddressActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ipaddressActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ipaddressActionPerformed

    private void connectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectActionPerformed
        try {
            videoTrans = new Socket(ipaddress.getText(), Integer.parseInt( port.getText()));
            out = new PrintWriter(videoTrans.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                                        videoTrans.getInputStream()));

            //Se logro la conexión.
            feedback.setText("Conexión establecida con exito.");
            disconnect.setEnabled(true);
            connect.setEnabled(false);

        } catch (UnknownHostException e) {
            feedback.setText("Error al intentar conectarse con: "+ipaddress.getText());
            //System.exit(1);
        } catch (Exception e) {
            feedback.setText("Error al intentar establecer la conexión");
            //System.exit(1);
        }
    }//GEN-LAST:event_connectActionPerformed

    public boolean doDisconnect(){
        try {
            videoTrans.close();
            out = null;
            in = null;
            
            //desconexion exitosa...
            disconnect.setEnabled(false);
            connect.setEnabled(true);
            feedback.setText("Desconexion realizada con exito.");

            return true;

        } catch (IOException ex) {
            feedback.setText("Error al intentar desconectar, reinicie el programa.");
            return false;
        }
    }

    public void clearFeedBack(){
        feedback.setText(" ");
    }

    private void disconnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_disconnectActionPerformed
        doDisconnect();
    }//GEN-LAST:event_disconnectActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton connect;
    private javax.swing.JButton disconnect;
    private javax.swing.JLabel feedback;
    private javax.swing.JTextField ipaddress;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField port;
    // End of variables declaration//GEN-END:variables
    //Variables declaradas para poder hacer la conexion
    public Socket videoTrans = null;
    public PrintWriter out = null;
    public BufferedReader in = null;
}
